$(document).ready(function(){
	var page = 1;
	var rows = 10;
	loadComment(page,rows,"","","","","");
    $(function(){
        $(".search-box input").focus(function(){
            $(this).parent('div').css('border','2px solid #F5C63E');
        }).blur(function(){
            $(this).parent('div').css('border','2px solid #C0C0C0');
        });
    });
    $(function(){
        $(".state li").click(function(){
            $(this).addClass('active').siblings('li').removeClass('active');
            var state=$(this).html();
        })
    });
    //w调用日期选择插件
    $('#datePicker').daterangepicker();
    $('body').on('click','.applyBtn',function(){
        var date = $('#datePicker').val();
        arr=date.split(" - "); 
        var startTime = arr[0];
        var endTime = arr[1];
        $('#orderCode').val("");
        loadComment(page,rows,"",startTime,endTime,"","");
     });
    // 搜索框搜索
    $('.search-box').on('click','span',function(){
    	var orderCode = $('#orderCode').val();
    	$('#datePicker').val("");
    	loadComment(page,rows,orderCode,"","","","");
    });
    // 订单状态搜索
    $('#orderState').on('click','li',function(){
    	$('#orderCode').val("");
    	$('#datePicker').val("");
    	var orderState = $(this).attr('data-id');
    	loadComment(page,rows,"","","",orderState,"");
    });
//    发货和详情
    $('#orderList').on('click','button',function(){
    	var id = $(this).parent().parent().attr('data-id');
    	if($(this).hasClass('detail')){
    		orderDetail(id);
    	}else if($(this).hasClass('delivery')){
    		delivery(id);
    	}
    })
});
//页面加载获取数据并分页
function loadComment(page,rows,orderCode,startTime,endTime,orderState,paymentState) {
	$.ajax({
		type: "post",
		url: path + "/orderlang/order/orderList",
		data: {
			page : page,
			rows : rows,
			orderCode:orderCode,
			startTime:startTime,
			endTime:endTime,
			orderState:orderState,
			paymentState:paymentState
		},
		dataType: 'json',
		success: function(data){
			console.log(data);
			var orderList = data.state == 1?data.data:data.state;
			var commentContent = '';
			if(orderList) {
				for(var i=0; i<orderList.rows.length; i++) {
					var comment = orderList.rows[i];
					commentContent += '<tr data-id="'+comment.id+'">';
					commentContent += '<td><s></s></td>';
					commentContent += '<td>'+comment.orderCode+'</td>';
					commentContent += '<td>'+comment.orderTime+'</td>';
					commentContent += '<td>'+comment.customerName+'</td>';
					if(comment.customerLevel == 'normal'){
						commentContent += '<td class="order-state"><p>普通用户</p></td>';
					}else if(comment.customerLevel == 'gold'){
						commentContent += '<td class="order-state"><p>金卡会员</p></td>';
					}else if(comment.customerLevel == 'diamond'){
						commentContent += '<td class="order-state"><p>钻石会员</p></td>';
					}else if(comment.customerLevel == 'shareholder'){
						commentContent += '<td class="order-state"><p>股东</p></td>';
					};
					commentContent += '<td>'+comment.amount+'</td>';
					if(comment.orderState == 0){
						commentContent += '<td class="order-state"><p class="c2017">处理中</p></td>';
					}else if(comment.orderState == 1){
						commentContent += '<td class="order-state"><p class="c2018">已关闭</p></td>';
					};
					if(comment.paymentState == 0){
						commentContent += '<td class="order-state"><p class="c2019">付款中</p></td>';
					}else if(comment.paymentState == 1){
						commentContent += '<td class="order-state"><p class="c2020">已结清</p></td>';
					};
					if(comment.applyType == 'cash'){
						commentContent += '<td class="order-state"><p>现金</p></td>';
					}else if(comment.applyType == 'transfer'){
						commentContent += '<td class="order-state"><p>转账</p></td>';
					}else if(comment.applyType == 'alipay'){
						commentContent += '<td class="order-state"><p>支付宝</p></td>';
					};
					commentContent += '<td>'+comment.remark+'</td>';
					commentContent += '<td>';
					commentContent += '<button class="detail">查看</button>';
					if(comment.orderState == 0){
						commentContent += '<button class="delivery">发货</button>';
					}
					commentContent += '</td>';
					commentContent += '</tr>'
				}
				$('#orderList').html(commentContent);
				var dataPage = orderList.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+');">尾页</a>';
				}
			}else{
				$('#orderList').html('');
				$(".quotes").html('');
			}
			$(".quotes").html(pageContent);
		},
	});
}
/**
 * 查看订单详情
 */
function orderDetail(id){
	window.location.href = path+"/distributor/orderDetail?id="+id;
}
/**
 * 发货
 */
function delivery(id){
	window.location.href = path + "/distributor/newSendOrder?id="+id;
}


